package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.commons.utils.BeanCopyUtil;
import com.woniuxy.controller.form.GetAllStoreRegisterForm;
import com.woniuxy.dao.model.StoreRegister;
import com.woniuxy.dao.StoreRegisterDao;
import com.woniuxy.service.StoreRegisterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.dto.GetMyRegisterStoreDto;
import com.woniuxy.service.dto.PageStoreRegisterDto;
import com.woniuxy.service.dto.StoreRegisterDto;
import com.woniuxy.service.dto.UserDetailsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商家注册表 服务实现类
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@Service
public class StoreRegisterServiceImpl extends ServiceImpl<StoreRegisterDao, StoreRegister> implements StoreRegisterService {

    @Resource
    private StoreRegisterDao storeRegisterDao;

    /**
     * 商家查询自己认证记录的实现
     * @return
     */
    @Override
    public List<GetMyRegisterStoreDto> getMyRegisterStore() {
        UserDetailsDto userDetailsDto = (UserDetailsDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        String storeName = userDetailsDto.getUsername();

        List<StoreRegister> listStoreRegister = storeRegisterDao.selectList(new QueryWrapper<StoreRegister>().eq("storeName", storeName));

        List<GetMyRegisterStoreDto> getMyRegisterStoreDtos = BeanCopyUtil.copyList(listStoreRegister, GetMyRegisterStoreDto.class);

        return getMyRegisterStoreDtos;
    }

    /**
     * 超级管理员查询所有商家认证记录的接口
     * @param gasrf
     * @return
     */
    @Override
    public PageStoreRegisterDto getAllStoreRegister(GetAllStoreRegisterForm gasrf) {
        //1.前置条件判断
        QueryWrapper<StoreRegister> qw = new QueryWrapper<>();

        if(gasrf.getStoreName() != null){
            qw.like("storeName",gasrf.getStoreName());
        }

        if(gasrf.getLinkman() != null){
            qw.like("linkman",gasrf.getLinkman());
        }

        if(gasrf.getStatus() != null){
            if(gasrf.getStatus() == 0 || gasrf.getStatus() == 1 || gasrf.getStatus() == 2){
                qw.eq("status",gasrf.getStatus());
            }
        }

        //2.分页查询
        IPage<StoreRegister> iPage = this.page(new Page<StoreRegister>(gasrf.getPageNum(), gasrf.getPageSize()), qw);

        PageStoreRegisterDto psrd = new PageStoreRegisterDto();

        List<StoreRegisterDto> srds = new ArrayList<>();

        if(iPage.getRecords() != null && iPage.getRecords().size()>0){
            srds = BeanCopyUtil.copyList(iPage.getRecords(),StoreRegisterDto.class);
        }

        psrd.setRegisters(srds);
        psrd.setIndexPage(iPage.getCurrent());
        psrd.setTotal(iPage.getTotal());

        return psrd;
    }


}
